package com.example.zqq.shoushipassword;

import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.zqq.shoushipassword.view.NinePointLineView;


/**
 * 修改和关闭手势密码
 */
public class ModifyGuestActivity extends AppCompatActivity {

    private LinearLayout nine_con;    //九宫格容器
    private TextView showInfo;
    private TextView mName;
    private TextView mPhone;
    private TextView mBottom;
    private LinearLayout mBack;

//    private Token mToken;
    private int flag=0;
    private NinePointLineView nv;             //九宫格View
    private AlertDialog mServiceDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resatrt);

        flag=getIntent().getExtras().getInt("flag");
//        mToken=new Token(ModifyGuestActivity.this);
        nv = new NinePointLineView(ModifyGuestActivity.this, flag);
        findView();
    }

    public void onResume(){
        savePwd();
        super.onResume();
    }

    public static int mNumber = 0; // 用来记录手势密码输入错误的次数，最大为5


    private void findView() {
        mBack=(LinearLayout)findViewById(R.id.back);
        nine_con = (LinearLayout)findViewById(R.id.nine_con);
        showInfo=(TextView)findViewById(R.id.show_set_info);
        mBottom=(TextView)findViewById(R.id.bottom);
        mName=(TextView)findViewById(R.id.name);
        mPhone=(TextView)findViewById(R.id.phone);

        nine_con.addView(nv);

        //取用户信息并显示
        SharedPreferences setting=getSharedPreferences("userInfo", MODE_PRIVATE);
        String name=setting.getString("userName", "");
        if (name == null || name.equals("")) {
            mName.setText("");
        } else {
            mName.setText(name);
        }
        if (setting.getString("userPhone", "") == null || setting.getString("userPhone", "").equals("")) {
            mPhone.setText("");
        } else {
            mPhone.setText(setting.getString("userPhone", "").substring(0, 3) + "*****" + setting.getString("userPhone", "").substring(8));
        }

        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });

        //底部提示按钮点击事件
        mBottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CallDialog();
            }
        });
    }

    private void CallDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        mServiceDialog = builder.create();
        mServiceDialog.show();
        mServiceDialog.setContentView(R.layout.service_dialog);

        mServiceDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        WindowManager.LayoutParams params = mServiceDialog.getWindow().getAttributes();
        params.gravity = Gravity.CENTER;
        mServiceDialog.getWindow().setAttributes(params);

        TextView sure;
        TextView cancle;
        TextView phone;
        sure=(TextView)mServiceDialog.findViewById(R.id.sure);
        sure.setText("重新登录");
        cancle=(TextView)mServiceDialog.findViewById(R.id.cancle);
        phone=(TextView)mServiceDialog.findViewById(R.id.service_phone);
        phone.setTextSize(13);
        phone.setTextColor(getResources().getColor(R.color.grey));
        phone.setText("忘记手势密码，需要重新登录");

        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mServiceDialog.dismiss();
            }
        });

        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ModifyGuestActivity.this, PayPhoneNumActivity.class);

                startActivityForResult(intent, PHONENUM);
            }
        });
    }

    public final int PHONENUM = 0x000000F4;
    public final static int wrongTimesOverlimited = 0x000002F0;

    //对手势密码进行保存和判断操作
    public void savePwd() {
        SharedPreferences shareDate = getSharedPreferences("GUE_PWD", 0);
        boolean is_second_error = shareDate.getBoolean("SECOND_ERROR", false);
        if (is_second_error) {
//            SmalltaurusApplication.mNumber++;
            mNumber++;
            if (mNumber > 4) {
                //当手势密码输入错误超过五次时，直接跳转到登录页面
//                mToken.clearShared(0);
                Intent intent = new Intent(this, PayPhoneNumActivity.class);

                startActivityForResult(intent, wrongTimesOverlimited);
            } else {
                showInfo.setTextColor(getResources().getColor(R.color.red));
                showInfo.setText("密码输入错误，还可以再输入" + (5 - mNumber) + "次");
            }
        }
    }


    @Override
    public void onBackPressed() {
        //返回不保存之前操作的状态
        SharedPreferences shareDate = getSharedPreferences("GUE_PWD", 0);
        shareDate.edit().putBoolean("SECOND_ERROR", false)
                .apply();
        mNumber=0;
//        SmalltaurusApplication.mNumber=0;
        super.onBackPressed();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 禁用Back键
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && flag == 2) {
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case PHONENUM:
                if (requestCode == wrongTimesOverlimited) {
//                    SmalltaurusApplication.choise=1;
                    ModifyGuestActivity.this.finish();
                    return;
                }
//                mToken.clearToggle();
//                SmalltaurusApplication.choise=1;
                ModifyGuestActivity.this.finish();
                break;
            default:
                break;
        }
    }


}
